package com.ywy.utils;

import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class CheckShareLinkEnableUtil {
    static CloseableHttpClient httpClient = HttpClients.createDefault();

    /**
     * 校验链接是否生效
     * @param link
     * @return
     */
    public static boolean checkLink(String link) {
        try {
            HttpGet httpget = new HttpGet(link.trim());
            httpget.setHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:50.0) Gecko/20100101 Firefox/50.0"); // 设置请求头消息User-Agent
            CloseableHttpResponse response = httpClient.execute(httpget);
            HttpEntity entity = response.getEntity(); // 获取返回实体
            String result = EntityUtils.toString(entity, "utf-8");
            if (result.contains("请输入提取码") || result.contains("分享无限制")) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return false;
    }
}
